package com.icinfo.cloud.provider.punish.config.controller;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.user.service.IMemberUserCommonService;
import com.icinfo.cloud.provider.punish.config.dto.ConfigDeptParamDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigDeptParamRequest;
import com.icinfo.cloud.provider.punish.config.dto.ConfigDeptSpecialParamDto;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigDeptParamKeyInfoVo;
import com.icinfo.cloud.provider.punish.config.vo.ConfigDeptParamVo;
import com.icinfo.framework.common.web.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：部门参数信息配置前端控制器
 *
 * @author xiangyuyu
 * @date 2022/5/16 14:49
 */
@RestController
@RefreshScope
@RequestMapping("/punish/config/deptParam")
@Slf4j
public class ConfigDeptParamController {

    @Resource
    private ISysDepartParamService sysDepartParamService;
    @Resource
    private IMemberUserCommonService memberUserCommonService;

    /**
     * 【V2.0.0】描述：查询已配置的部门关键信息列表
     *
     * @return {@link Result<List<ConfigDeptParamKeyInfoVo>>} 已配置的部门关键信息列表
     * @author xiangyuyu
     * @date 2022-05-16 16:16
     */
    @PostMapping("/getExistsDeptInfoList")
    @ApiOperation(value = "【V2.0.0】描述：查询已配置的部门关键信息列表", httpMethod = "POST")
    public Result<List<ConfigDeptParamKeyInfoVo>> getExistsDeptInfoList() {
        List<ConfigDeptParamKeyInfoVo> list = sysDepartParamService.getExistsDeptKeyInfoList();
        return Result.success(Constant.STRING_GET_SUCCESS, list);
    }

    /**
     * 【V1.3.1】描述：根据指定条件查询部门参数配置信息
     *
     * @param request {@link ConfigDeptParamRequest}
     * @return {@link Result<ConfigDeptParamVo>} 部门参数配置信息
     * @author xiangyuyu
     * @date 2022-05-16 16:16
     */
    @PostMapping("/getDeptConfigInfoWithSpecialCondition")
    @ApiOperation(value = "【V1.3.1】描述：根据指定条件查询部门参数配置信息", httpMethod = "POST")
    public Result<ConfigDeptParamVo> getDeptConfigInfoWithSpecialCondition(@RequestBody @Validated ConfigDeptParamRequest request) {
        ConfigDeptParamVo deptConfigInfo = sysDepartParamService.getDeptConfigInfoWithSpecialCondition(request);
        return Result.success(Constant.STRING_GET_SUCCESS, deptConfigInfo);
    }

    /**
     * 【V2.0.0】描述：保存部门基本参数配置信息
     *
     * @param saveDto {@link ConfigDeptParamDto}
     * @return {@link Result<String>} 保存结果
     * @author xiangyuyu
     * @date 2022-05-16 16:16
     */
    @PostMapping("/saveDeptBasicConfigInfo")
    @ApiOperation(value = "【V2.0.0】描述：保存部门基本参数配置信息", httpMethod = "POST")
    public Result<String> saveDeptBasicConfigInfo(@RequestBody @Validated ConfigDeptParamDto saveDto) {
        return sysDepartParamService.saveDeptBasicConfigInfo(saveDto);
    }

    /**
     * 【V2.0.0】描述：保存部门指定配置信息
     *
     * @param saveDto {@link ConfigDeptSpecialParamDto}
     * @return {@link Result<String>} 保存结果
     * @author xiangyuyu
     * @date 2022-05-16 16:16
     */
    @PostMapping("/saveDeptSpecialConfigInfo")
    @ApiOperation(value = "【V2.0.0】描述：保存部门指定配置信息", httpMethod = "POST")
    public Result<String> saveDeptSpecialConfigInfo(@RequestBody @Validated ConfigDeptSpecialParamDto saveDto) {
        return sysDepartParamService.saveDeptSpecialConfigInfo(saveDto);
    }

    /**
     * 获取是否展示修改按钮;1 展示 0 不展示
     *
     * @param
     * @return {@link Result}
     * @author liyafeng
     * @date 2022/9/5
     */
    @RequestMapping(value = "/getIsShowEditButton")
    public Result getIsShowEditButton() {
        String dataPermission = "";
        try {
            dataPermission = memberUserCommonService.getIsShowEditButton();
        } catch (Exception e) {
            log.info("失败:" + e.toString());
        }
        return Result.success("success", dataPermission);
    }

    /**
     * 描述: 根据登录人部门查询条线配置
     *
     * @return com.icinfo.framework.common.web.Result
     * @author zhanghongqiang
     * @Date 2022/10/28
     */
    @GetMapping("/getDeptLineConfig")
    @ApiOperation(value = "描述：根据登录人部门查询条线配置", httpMethod = "POST")
    public Result<List<String>> getDeptLineConfig() {
        List<String> lineCode = sysDepartParamService.getDeptLineConfig();
        if (ObjectUtils.isEmpty(lineCode)) {
            return Result.error(Constant.STRING_GET_FAIL);
        } else {
            return Result.success(Constant.STRING_GET_SUCCESS, lineCode);
        }
    }

    /**
     * 无效机构配置信息
     *
     * @param request
     * @return {@link Result< ConfigDeptParamVo>}
     * @author liyafeng
     * @date 2022/11/23
     */
    @PostMapping("/invalidSysDepartParam")
    public Result<String> invalidSysDepartParam(@RequestBody @Validated ConfigDeptParamRequest request) {
        int num = sysDepartParamService.invalidSysDepartParam(request);
        if (num > 0) {
            return Result.success("无效成功");
        }
        return Result.error("无效失败");
    }

    /**
     * 根据部门编码与机构编码获取对应的参数配置
     *
     * @param request
     * @return {@link Result< String>}
     * @author liyafeng
     * @date 2022/11/23
     */
    @PostMapping("/getSysDepartParamByOrgCodeAndDeptCode")
    public Result<SysDepartParam> getSysDepartParamByOrgCodeAndDeptCode(@RequestBody @Validated ConfigDeptParamRequest request) {
        SysDepartParam sysDepartParam = sysDepartParamService.getSysDepartParamByOrgCodeAndDeptCode(request.getOrgCode(), request.getDeptCode());
        return Result.success("获取成功", sysDepartParam);
    }


    /**
     * 获取签章编码
     *
     * @param request
     * @return
     */
    @PostMapping("/getSealCode")
    public Result<String> getSealCode(@RequestBody @Validated ConfigDeptParamRequest request) {
        return Result.success("获取成功", sysDepartParamService.getDeptConfigInfoWithSpecialCondition(request).getSealCode());
    }


}
